package com.qdxht.dispenser.ui.activity.main;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import com.hjq.toast.ToastUtils;
import com.qdxht.dispenser.base.BaseActivity;
import com.qdxht.dispenser.databinding.ActivityDeliverySettingsBinding;
import com.qdxht.dispenser.utils.ConfigUtil;

/**
 * 出货设置页面
 */
public class DeliverySettingsActivity extends BaseActivity<ActivityDeliverySettingsBinding> {

    // 默认出货长度
    private static final int DEFAULT_ROLLING_DISTANCE = 270;

    public static void start(Context context) {
        Intent intent = new Intent(context, DeliverySettingsActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected ActivityDeliverySettingsBinding getBinding() {
        return ActivityDeliverySettingsBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        // 设置保存按钮点击事件
        binding.btnSave.setOnClickListener(v -> saveSettings());

        // 设置重置按钮点击事件
        binding.btnReset.setOnClickListener(v -> resetSettings());
    }

    @Override
    protected void initData() {
        // 获取当前设置的出货长度
        int rollingDistance = ConfigUtil.getRollingDistance();
        // 显示到输入框
        binding.etRollingDistance.setText(String.valueOf(rollingDistance));
    }

    /**
     * 保存设置
     */
    private void saveSettings() {
        String distanceStr = binding.etRollingDistance.getText().toString().trim();
        if (TextUtils.isEmpty(distanceStr)) {
            ToastUtils.show("请输入出货长度");
            return;
        }

        try {
            int rollingDistance = Integer.parseInt(distanceStr);
            if (rollingDistance <= 0) {
                ToastUtils.show("出货长度必须大于0");
                return;
            }

            // 保存出货长度
            ConfigUtil.setRollingDistance(rollingDistance);
            ToastUtils.show("保存成功");
            finish();
        } catch (NumberFormatException e) {
            ToastUtils.show("请输入有效的数字");
        }
    }

    /**
     * 重置设置
     */
    private void resetSettings() {
        // 重置为默认值
        binding.etRollingDistance.setText(String.valueOf(DEFAULT_ROLLING_DISTANCE));
    }
}

